<?php

/**
 * This is the model class for table "{{widget}}".
 *
 * The followings are the available columns in table '{{widget}}':
 * @property integer $id
 * @property string $name
 * @property string $widget_type
 * @property string $description
 * @property string $position
 * @property integer $status
 * @property string $created_by
 * @property string $create_date
 * @property string $updated_by
 * @property string $update_date
 * @property string $params
 * @property string $language
 */
class Widget extends CActiveRecord {

    public $customObject;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Widget the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{widget}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	// NOTE: you should only define rules for those attributes that
	// will receive user inputs.
	return array(
	    array('name, widget_type, position, status', 'required'),
	    array('status', 'numerical', 'integerOnly' => true),
	    array('name', 'length', 'max' => 100),
	    array('widget_type, created_by, updated_by, language', 'length', 'max' => 255),
	    array('description', 'length', 'max' => 1000),
	    array('position', 'length', 'max' => 20),
	    array('create_date, update_date', 'safe'),
	    // The following rule is used by search().
	    // Please remove those attributes that should not be searched.
	    array('id, name, widget_type, description, position, status, created_by, create_date, updated_by, update_date, params, language', 'safe', 'on' => 'search'),
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'id' => 'ID',
	    'name' => 'Name',
	    'widget_type' => 'Widget Type',
	    'description' => 'Description',
	    'position' => 'Position',
	    'status' => 'Status',
	    'created_by' => 'Created By',
	    'create_date' => 'Create Date',
	    'updated_by' => 'Updated By',
	    'update_date' => 'Update Date',
	    'params' => 'Params',
	    'language' => 'Language',
	);
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;

	$criteria->compare('id', $this->id);
	
	if (isset($this->name)) {
	    $this->name = trim($this->name);
	}
	
	$criteria->addSearchCondition('name', $this->name);
	$criteria->compare('widget_type', $this->widget_type, true);
	$criteria->compare('description', $this->description, true);
	$criteria->compare('position', $this->position, true);
	$criteria->compare('status', $this->status);
	$criteria->compare('created_by', $this->created_by, true);
	$criteria->compare('create_date', $this->create_date, true);
	$criteria->compare('updated_by', $this->updated_by, true);
	$criteria->compare('update_date', $this->update_date, true);
	$criteria->compare('params', $this->params, true);
	$criteria->compare('language', $this->language, true);

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	));
    }

    protected function beforeSave() {
	if (parent::beforeSave()) {
	     $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);
	    $logedInUser = User::model()->findByPk(Yii::app()->user->id);

	   

	    $this->update_date = $current_date;
	    $this->updated_by = $logedInUser->username;
	    return true;
	} else {
	    return false;
	}
    }
    protected function afterFind() {
	parent::afterFind();

	$updateDateTimeSpan = strtotime($this->update_date);
	$this->update_date = date(LGConstant::DATE_FORMAT, $updateDateTimeSpan);

	if ($this->update_date == LGConstant::DATE_DEFAULT) {
	    $this->update_date = '';
	}
	
	$this->customObject = array();

	//TODO: decode params to silde Items
	if ($this->params == NULL || empty($this->params)) {
	    
	} else {

	    if ($this->widget_type === LGConstantWidgetType::BANNER) {
		$slideItems = json_decode($this->params, true);

		foreach ($slideItems as $slideItem) {
		    $s = new SlideItem;
		    $s->attributes = $slideItem;

		    array_push($this->customObject, $s);
		}
	    } else if ($this->widget_type === LGConstantWidgetType::TAB) {
		$slideItems = json_decode($this->params, true);

		foreach ($slideItems as $slideItem) {
		    $s = new TabItem;
		    $s->attributes = $slideItem;

		    array_push($this->customObject, $s);
		}
	    } else if ($this->widget_type == LGConstantWidgetType::CUSTOM_HTML) {
		    $this->customObject = new CustomHTML;
		    $this->customObject->attributes = json_decode($this->params, true);
		    
		    $this->customObject->content = strip_tags_content($this->customObject->content, '<script>', true);
		    
	    }
	}
    }

}